package com.nowcoder.community.util;

/**
 * 用于生成Redis的统一key
 * @author 花木凋零成兰
 * @date 2024/3/17 18:23
 */
public class RedisKeyUtil {

    private static final String SPLIT = ":";

    // 前缀
    private static final String PREFIX_ENTITY_LIKE = "like:entity";
    private static final String PREFIX_USER_LIKE = "like:user";
    private static final String PREFIX_FOLLOWEE = "followee";   // 用户关注某实体
    private static final String PREFIX_FOLLOWER = "follower";   // 某用户粉丝
    private static final String PREFIX_KAPTCHA = "kaptcha";     // 验证码
    private static final String PREFIX_TICKET = "ticket";
    private static final String PREFIX_USER = "user";
    private static final String PREFIX_UV = "uv";   // 网站访客数
    private static final String PREFIX_DAU = "dau"; // 网站日活跃用户
    private static final String PREFIX_POST = "post";   // 分数变化的帖子
    private static final String PREFIX_INDEX = "index"; // 首页按热度排名的 二级缓存

    /**
     * 获取实体的赞 的key
     * @param entityType
     * @param entityId
     * @return
     */
    // like:entity:entityType:entityId -> set(给我点赞的userId)
    public static String getEntityLikeKey(int entityType, int entityId) {
        return PREFIX_ENTITY_LIKE + SPLIT + entityType + SPLIT + entityId;
    }

    /**
     * 某个用户所获得的赞
     * @param userId
     * @return
     */
    public static String getUserLikeKey(int userId) {
        return PREFIX_USER_LIKE + SPLIT + userId;
    }

    /**
     * 获取某个用户关注的实体key
     * @param userId
     * @param entityType
     * @return
     */
    // followee:userId:entityType -> zset(entityId, now)
    public static String getFolloweeKey(int userId, int entityType) {
        return PREFIX_FOLLOWEE + SPLIT + userId + SPLIT + entityType;
    }

    /**
     * 某个实体拥有的粉丝
     * @param entityType
     * @param entityId
     * @return
     */
    // follower:entityType:entityId -> zset(userId, now)
    public static String getFollowerKey(int entityType, int entityId) {
        return PREFIX_FOLLOWER + SPLIT + entityType + SPLIT + entityId;
    }

    /**
     * 登录验证码
     * @param owner
     * @return
     */
    public static String getKaptchaKey(String owner) {
        return PREFIX_KAPTCHA + SPLIT + owner;
    }

    /**
     * 登录凭证
     * @param ticket
     * @return
     */
    public static String getTicketKey(String ticket) {
        return PREFIX_TICKET + SPLIT + ticket;
    }

    public static String getUserKey(int userId) {
        return PREFIX_USER + SPLIT + userId;
    }

    /**
     * 获取单日UV 的key
     * @param date
     * @return
     */
    public static String getUVKey(String date) {
        return PREFIX_UV + SPLIT + date;
    }

    /**
     * 获得区间内的 UV
     * @param startDate
     * @param endDate
     * @return
     */
    public static String getUVKey(String startDate, String endDate) {
        return PREFIX_UV + SPLIT + startDate + SPLIT + endDate;
    }

    /**
     * 获取单日活跃用户 DAU 的Key
     * @param data
     * @return
     */
    public static String getDAUKey(String data) {
        return PREFIX_DAU + SPLIT + data;
    }

    /**
     * 获取区间内的活跃用户
     * @param startDate
     * @param endDate
     * @return
     */
    public static String getDAUKey(String startDate, String endDate) {
        return PREFIX_DAU + SPLIT + startDate + SPLIT + endDate;
    }

    /**
     * 帖子分数
     * @return
     */
    public static String getPostScoreKey() {
        return PREFIX_POST + SPLIT + "score";
    }

    /**
     * 获取首页帖子热度排名 key
     * @param key
     * @return
     */
    public static String getDiscussPostListKey(String key) {
        return PREFIX_INDEX + SPLIT + key;
    }

    /**
     * 获取首页 帖子总数 key
     * @param id
     * @return
     */
    public static String getDiscussPostRowsKey(int id) {
        return PREFIX_INDEX + SPLIT + id;
    }

}
